<?php
/**
 * Created by PhpStorm.
 * User: LiJia
 * Date: 2016/4/18
 * Time: 22:09
 */

class RedisUtils
{
    static public $redis_userinfocache=null;


    static public function connect()
    {
        if(empty(RedisUtils::$redis_userinfocache))
            RedisUtils::$redis_userinfocache = new Redis();

        $host=Yii::$app->params['RedisHost'];
        $port=Yii::$app->params['RedisPort'];
        if(!RedisUtils::$redis_userinfocache->pconnect($host,$port,30)){
            throw new Exception('connect.Host:'.$host.':'.$port.' is fail.');
            return null;
        }
    }

    static public function close()
    {
        if(!empty(RedisUtils::$redis_userinfocache))
            RedisUtils::$redis_userinfocache->close();
    }

    static public function get_manager_role($manageid)
    {
        RedisUtils::connect();
        RedisUtils::$redis_userinfocache->select(Yii::$app->params['RedisDB']);
        $result=null;
        if(!empty($manageid))
        {

            $sdata=RedisUtils::$redis_userinfocache->GET('m_'.$manageid);

                return $sdata;
        }
        return $result;
    }
    //写入redis
    static public function set_manager_role($manageid,$josnrole)
    {
        if(empty(RedisUtils::$redis_userinfocache)){
            RedisUtils::$redis_userinfocache= new Redis();
            $host=Yii::$app->params['RedisHost'];
            $port=Yii::$app->params['RedisPort'];
            if(!RedisUtils::$redis_userinfocache->pconnect($host,$port,30)){
                throw new Exception('set_manager_role().Host:'.$host.':'.$port.' is fail.');
                return null;
            }
        }
        $result=false;
        if(isset($manageid))
        {
            RedisUtils::$redis_userinfocache->select(Yii::$app->params['RedisDB']);


            RedisUtils::$redis_userinfocache->set('m_'.$manageid,$josnrole);
            RedisUtils::$redis_userinfocache->EXPIRE('m_'.$manageid,'3000');
            $result=true;
        }
        RedisUtils::$redis_userinfocache->close();
        return $result;
    }
}